Ext.onReady(function() {
	Ext.Loader.setPath("org.jks", "view/common");
	var selectId = null;
	var loadMask = new Ext.LoadMask(Ext.getBody(), {
				msg : "请稍后，正在处理..."
			});
	var toolbar = Ext.create('Ext.toolbar.Toolbar', {
		dock : 'top',
		items : [{
					text : '返回',
					xtype : 'button',
					listeners : {
						click : function() {
							location.href = "/sysMgr/privilege_mgr";
						}
					}
				}, '-', {
					text : '删除',
					xtype : 'button',
					listeners : {
						click : function() {
							loadMask.show();
							var grid = Ext.getCmp("role_grid");
							var selectionModel = grid.getSelectionModel();
							var models = selectionModel.getSelection();
							var ids = [];
							for (var i = 0; i < models.length; i++) {
								ids[i] = models[i].get("Id");
							}
							Ext.Ajax.request({
								url : '/role/delete',
								type : 'POST',
								params : {
									ids : ids
								},
								callback : function(options, success, response) {
									loadMask.hide();
									if (success) {
										var result = Ext
												.decode(response.responseText);
										if (result.success) {
											Ext.getCmp("role_grid").getStore()
													.reload();
										} else {
											Ext.Msg.alert('错误', result.msg);
										}
									}
								}
							});
						}
					}
				}, {
					text : '刷新',
					xtype : 'button',
					listeners : {
						click : function() {
							Ext.getCmp("role_grid").getStore().reload();
						}
					}
				}, {
					xtype : 'button',
					text : '添加',
					listeners : {
						click : function() {
							Ext.create('org.jks.AddRoleWindow').show();
						}
					}
				}]
	});
	var store = Ext.create('Ext.data.Store', {
				fields : ['Id', 'Name', 'Desp', 'privileges'],
				autoLoad : true,
				proxy : {
					type : 'ajax',
					url : '/role/all',
					reader : {
						type : 'json',
						root : 'data.rows'
					}
				}
			});
	var privilegeStore = Ext.create('Ext.data.Store', {
				fields : ['Id', {
							name : "Privilege",
							type : "object"
						}, {
							name : "Selected",
							type : 'boolean'
						}]
			});
	var privilegeGrid = Ext.create('Ext.grid.Panel', {
				store : privilegeStore,
				disableSelection : true,
				hideHeaders : true,
				border : true,
				bodyStyle : "border-left:none;",
				columns : [{
							xtype : 'checkcolumn',
							width : 40,
							dataIndex : 'Selected'
						}, {
							text : '',
							flex : 1,
							dataIndex : 'Privilege',
							renderer : function(v, meta, record) {
								return v.Name;
							}
						}]
			});
	var grid = Ext.create('Ext.grid.Panel', {
				id : 'role_grid',
				selType : 'checkboxmodel',
				store : store,
				selModel : {
					mode : "SINGLE"
				},
				region : 'center',
				columns : [{
							text : '序号',
							xtype : 'rownumberer',
							width : 40,
							height : 35,
							sortable : false
						}, {
							text : '名称',
							width : 200,
							dataIndex : 'Name'
						}, {
							text : '描述',
							dataIndex : 'Desp',
							flex : 1
						}],
				listeners : {
					select : function(self, record, index, eOpts) {
						var privs = record.get("privileges");
						var store = privilegeGrid.getStore();
						store.loadData(privs);
					},
					afterlayout : function(self, layout, eOpts) {
						if (selectId == null) {
							self.getSelectionModel().select(store.first());
						} else {
							self.getSelectionModel().select(store.findRecord(
									"Id", selectId));
						}
					}
				},
				bbar : Ext.create('Ext.PagingToolbar', {
							store : store,
							border: false,
							displayInfo : true,
							displayMsg : '显示{0} - {1} / 总数 {2}',
							emptyMsg : "没数据"
						})
			});

	var prPanel = Ext.create('Ext.panel.Panel', {
		region : 'east',
		width : 300,
		border : false,
		bodyBorder : false,
		layout : 'fit',
		header : {
			height : 36
		},
		title : '角色权限表',
		dockedItems : [{
			xtype : 'panel',
			bodyStyle : 'border:1px solid silver;border-left:none;',
			dock : 'bottom',
			items : [{
				xtype : 'button',
				margin : '5 5',
				text : '保存',
				listeners : {
					click : function() {
						loadMask.show();
						var selectionModel = grid.getSelectionModel();
						var models = selectionModel.getSelection();
						if (Ext.isEmpty(models)) {
							loadMask.hide();
							Ext.Msg.alert('错误', "保存失败");
							return;
						}
						var id = models[0].get("Id");
						selectId = id;
						var privs = privilegeStore.data.items;
						var privsArr = [];
						if (!Ext.isEmpty(privs)) {
							for (var i = 0; i < privs.length; i++) {
								if (privs[i].data.Selected) {
									Ext.Array.include(privsArr,
											privs[i].data.Privilege.Id);
								}
							}
						} else {
							loadMask.hide();
							Ext.Msg.alert('错误', "保存失败");
							return;
						}
						var params = {
							RoleId : id,
							privileges : privsArr
						};
						Ext.Ajax.request({
									url : '/role/update_priv',
									type : 'POST',
									params : params,
									callback : function(options, success,
											response) {
										loadMask.hide();
										if (success) {
											var ar = Ext
													.decode(response.responseText);
											if (ar.success) {
												Ext.Msg.alert('信息', "保存成功");
												var grid = Ext
														.getCmp("role_grid");
												var store = grid.getStore();
												store.reload();
												return;
											}
										}
										Ext.Msg.alert('错误', "保存失败");
									}
								});

					}
				}
			}]
		}],
		items : [privilegeGrid]
	});

	var panel = Ext.create("Ext.panel.Panel", {
				title : '角色管理',
				border : false,
				bodyBorder : false,
				layout : 'fit',
				dockedItems : [toolbar],
				items : [{
							xtype : 'container',
							margin : 5,
							layout : 'border',
							items : [grid, prPanel]
						}]
			});

	Ext.create('Ext.Viewport', {
				layout : 'fit',
				style : 'background:none;background-color:white;',
				items : [panel]
			});
});